﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using MSIACCSL2.ViewModels;
using MSIACCSL2.Views.FormSearch;
using MSIACCSL2.Views.Inventory;
using MSIACCSL2.Views.PreSalesOrder;
using WcfService1.DTO;

namespace MSIACCSL2.Views.DeliveryOrder
{
    public partial class FrmAdd : Page
    {
        private InventorySearch si;

        public DeliveryOrderViewModel VM
        {
            get { return (DeliveryOrderViewModel)this.DataContext; }
        }

        public DeliveryOrderWrapViewModel WMW
        {
            get { return (this.Resources["POW"] as DeliveryOrderWrapViewModel); }
        }

        public FrmAdd()
        {
            InitializeComponent();

            this.Loaded += (x,y) =>
                               {
                                   (this.Resources["POW"] as DeliveryOrderWrapViewModel).DeliveryOrderViewModel =
                                        (DeliveryOrderViewModel)this.DataContext;

                                   cboSO.Focus();
                               };

            txtSerialNumber.KeyDown += (o, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    WMW.GetInventory2(txtSerialNumber.Text,cboWarehouse.SelectedItem as WarehouseDTO);
                    txtSerialNumber.Text = string.Empty;
                }
            };

            this.KeyDown += (x, y) =>
            {
                if (y.Key == Key.S && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.SaveDeliveryOrder(VM.DeliveryOrder);
                else if (y.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)
                    VM.Cancel(null);
                else if (y.Key == Key.A && Keyboard.Modifiers == ModifierKeys.Control)
                    WMW.AddDeliveryOrderDetail(null);
                else if (y.Key == Key.D && Keyboard.Modifiers == ModifierKeys.Control)
                    WMW.DeleteDeliveryOrderDetail(null);
            };

            
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void btnFind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            si = new InventorySearch();
            si.Closed += new EventHandler(si_Closed);
            si.Show();
        }

        void si_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            if (inv != null) txtSerialNumber.Text = inv.SerialNumber;
            txtSerialNumber.Focus();
        }

        private void TextBox_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
        	(sender as TextBox).SelectAll();
        }

        private void cboCostCenter_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            //if (String.IsNullOrEmpty(VM.DeliveryOrder.DONo))
            //    VM.GetLastNoDeliveryOrder(cboCostCenter.SelectedValue);
            if (cboCostCenter.SelectedValue != null)
                VM.GetSalesOrderList(cboCostCenter.SelectedValue);
        }

        private void txtNoLog_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            (sender as TextBox).SelectAll();
        }

        private void cboWarehouse_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cboWarehouse.SelectedItem != null)
                VM.WarehouseSelected = (cboWarehouse.SelectedItem as WarehouseDTO);
        }

        //Lookup Inventory
        private void btnfind_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            InventorySearch inventorySearch = new InventorySearch();
            inventorySearch.Closed += new EventHandler(inventorySearch_Closed);
            inventorySearch.Show();
        }


        //Search Inventory Finished
        void inventorySearch_Closed(object sender, EventArgs e)
        {
            var inv = (sender as InventorySearch).Inventory;
            var doDetail = Grid1.SelectedItem as DeliveryOrderDetailDTO;
            doDetail.Inventory = inv;
            Grid1.Focus();
        }

        private void TextBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
        	if (e.Key == Key.Enter)
        	{
                if (VM.DeliveryOrder.CostCenter == null)
                {
                    MessageBox.Show("CostCenter harus dipilih dulu");
                    return;
                }
        	    VM.LoadDOForSearch((sender as TextBox).Text);
        	}
        }

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
        	var DO = ((sender as Button).DataContext as DeliveryOrderDTO);
            txtDoCopy.Text = DO.DONo;
            VM.gridDOVisibility = Visibility.Collapsed;
            VM.LoadDOfromDO(DO);
        }

        private void TextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            //DeliveryOrderDetailDTO doDetail = (sender as TextBox).DataContext as DeliveryOrderDetailDTO;
            //if (doDetail != null)
            //    if (doDetail.Inventory.Item.IsRounded)
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:N}", doDetail.QtyUnit);
            //    }
            //    else
            //    {
            //        (sender as TextBox).Text = string.Format(System.Globalization.CultureInfo.CurrentUICulture, "{0:0.0000}", doDetail.QtyUnit);
            //    }
        }

        private void txtNoLog_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                WMW.GetInventory(((TextBox)sender).DataContext);
        }

        private void DatePicker_SelectedDateChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (cboCostCenter.SelectedValue != null)
            {
                if (VM.DeliveryOrder != null) VM.GetSalesOrderList(VM.DeliveryOrder.CostCenter);
            } 
        }

        private void btnUpload_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            VM.LoadDetailFromFile(null);
        }

      

    }
}

